package com.online.studyonline.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.online.studyonline.mapper.NoteMapper;
import com.online.studyonline.pojo.Course;
import com.online.studyonline.pojo.Note;
import com.online.studyonline.pojo.ResultT;
import com.online.studyonline.service.CourseService;
import com.online.studyonline.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author urts
* @description 针对表【note】的数据库操作Service实现
* @createDate 2024-09-26 20:07:03
*/
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note>
    implements NoteService {

    @Autowired
    private NoteMapper noteMapper;
    @Autowired
    private CourseService courseService;

    @Override
    public ResultT<List<Note>> getNoteByUserId1(Integer id) {
        List<Note> noteList = noteMapper.getNoteByUserId1(id);
        return ResultT.success(noteList);
    }

    @Override
    public ResultT<List<Note>> getNoteByUserId(Integer id) {
        QueryWrapper<Note> wrapper = new QueryWrapper<Note>()
                .eq("user_id",id);

        List<Note> noteList = list(wrapper);
        return ResultT.success(noteList);
    }

    @Override
    public ResultT<String> addNote(String courseName, Integer userId, String noteText) {
        Note note = new Note();
        note.setUserId(userId);
        note.setNoteText(noteText);
        Course course = courseService.getByName(courseName);
        note.setCourseId(course.getCourseId());
        save(note);
        return ResultT.success("创建成功");
    }

    @Override
    public ResultT<String> delNote(Integer noteId) {
        removeById(noteId);
        return ResultT.success("删除成功");
    }

    @Override
    public List<Note> getAll() {
        return noteMapper.getAll();
    }
}




